package net.aiion.weave.impl.ui;

import java.util.Objects;
import java.util.UUID;

import net.aiion.weave.spi.cards.annotations.Access;
import net.aiion.weave.spi.cards.annotations.Property;
import net.aiion.weave.spi.injection.annotations.Inject;
import net.aiion.weave.spi.naming.Context;
import net.aiion.weave.spi.naming.annotations.Bound;
import net.aiion.weave.spi.ui.Component;
import net.aiion.weave.spi.ui.Container;
import net.aiion.weave.util.cryptography.SHA1;

public abstract class AbstractComponent implements Component
{
    private final String id;

    @Inject
    private Context context;

    private Container parent;

    private boolean visible;

    public AbstractComponent() {
        visible = true;
        id = SHA1.calculateString(String.valueOf(System.currentTimeMillis()) + UUID.randomUUID().toString());
    }

    @Bound
    public void bindComponent() {
        context.bind(getId(), this);
    }

    @Override
    @Property(value = Access.READ_ONLY, name = "id")
    public String getId() {
        return id;
    }

    @Override
    public void setParent(final Container aContainer) {
        if (!Objects.equals(parent, aContainer)) {
            parent = aContainer;
            parent.addComponent(this);
        }
    }

    @Override
    public Container getParent() {
        return parent;
    }

    @Override
    public void setVisible(final boolean aVisible) {
        visible = aVisible;
    }

    @Override
    @Property(value = Access.READ_ONLY, name = "visible")
    public boolean isVisible() {
        return visible;
    }
}
